home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / JPEG Convert 1.0 Source / JPEG Convert3.c < prev   
Encoding:
C/C++ Source or Header  |  1993-02-27  |  14.9 KB  |  526 lines  |  [TEXT/KAHL]

  1. /*
  2.  * JPEG Convert3.c
  3.  *
  4.  * Copyright (C) 1992, James H. Brunner.
  5.  *
  6.  * This file is part of the "JPEG Convert" program.  The JPEG Convert program signature ('Ijgp')
  7.  * and unique file types ('TARG', 'RLE ', 'PPM ') are registered with Apple by the author.
  8.  *
  9.  * The JPEG Convert program is an image format conversion program that utilizes the software of
  10.  * the Independent JPEG Group.  The JPEG Convert program is essentially a Macintosh user interface
  11.  * around the Independent JPEG Group's code.  The author of JPEG Convert maintains a copyright to
  12.  * the interface software only.  For conditions of distribution and use of the Independent JPEG
  13.  * Group's software, refer to the README file contained in the Independent JPEG Group's distribution
  14.  * package.
  15.  *
  16.  * (Further use of the word 'software' refers only to the source files for implementing this user
  17.  *  interface, including the resource file which does not include this comment. It does NOT refer
  18.  *  to the Independent JPEG Group's code.)
  19.  *
  20.  * The conditions for distribution of this software are as follows:
  21.  *        This software may be freely distributed provided that it is not distributed for profit; a
  22.  *        nominal copying fee may be charged.
  23.  *
  24.  *        Any distribution of this software must contain all original copyright notices.
  25.  *
  26.  * The conditions for use of this software IN FULL are as follows:
  27.  *        This software may be used in full by any person or business provided that the person or
  28.  *        business is not seeking profits directly from the use of this software.
  29.  *
  30.  * The conditions for use of this software IN PART are as follows:
  31.  *        Any person or business may copy and utilize portions of this software in other products
  32.  *        provided that a note that "portions of the software are copyright by James H. Brunner"
  33.  *        is included in the software AND the resultant software is not intended to be distributed
  34.  *        for profit.
  35.  *
  36.  *        If SOURCE for projects containing portions of this code is not to be made available
  37.  *        with the resultant programs, an additional note that "portions of the software are 
  38.  *        copyright James H. Brunner" must be included in some visable user documentation.
  39.  *
  40.  * Bottom line:  I give it away free; I don't want you selling it.  You can use the source if
  41.  * you wish, but don't sell it.  If you use my work, give me credit for it.
  42.  */
  43.  
  44. /*
  45.  * JPEG Convert3.c
  46.  *
  47.  * This is part 3 of the Macintosh GUI (Graphical User Interface).  It is intended to be
  48.  * compiled on a Macintosh computer with Think C.  This file contains the about box and
  49.  * help.  "JPEG Convert1.c" and "JPEG Convert2.c" contain the rest.
  50.  */
  51.  
  52.  
  53. #include "JPEG Convert.h"
  54.  
  55. #define ABOUT_DLOG    129
  56. #define ABOUT_TEXT    128
  57. #define ABOUT_CREDIT    129
  58.  
  59. #define WF_PICT    128
  60. #define JIM_BW    129
  61. #define TAM_BW    130
  62. #define    JIM_16    131
  63. #define TAM_16    132
  64.  
  65. #define CREDIT_OFFSET    60
  66.  
  67. enum {
  68.         kAboutCredit=2,
  69.         kAboutScroll,
  70.         kAboutText,
  71.         kAboutOKBorder,
  72.         kJimPic,
  73.         kTamPic
  74.     };
  75.  
  76. extern Boolean    gNewDialogMgr;        /* EXTERN: TRUE if the system 7 dialog manager is available */
  77. extern Boolean    gAppleEvents;        /* EXTERN: TRUE if the system 7 apple event facility is available */
  78. extern Boolean    gColorQuickdraw;    /* EXTERN: TRUE if color qd and main screen > 4 colors */
  79. extern Boolean    gFSSpecSupport;        /* EXTERN: TRUE if machine supports FSSpecs */
  80.  
  81. static Handle    gHandleSink;
  82. static TEHandle    gHelpText;
  83. static int        gHelpLine;        /* (1 based) */
  84.  
  85.  
  86. METHODDEF pascal void
  87. AboutTextItem (WindowPtr d, short dinum)
  88. {
  89.     short    itemType;
  90.     Rect    textRect;
  91.  
  92.     GetDItem(d, kAboutText, &itemType, &gHandleSink, &textRect);
  93.     PenSize(1,1);
  94.     FrameRect(&textRect);
  95.     
  96.     if (gHelpText != NULL) {
  97.         InsetRect(&textRect, 4, 2);
  98.         TEUpdate(&textRect, gHelpText);
  99.     } else {
  100.         PicHandle    picHand;
  101.         int            picHeight, picWidth;
  102.         
  103.         picHand = (PicHandle)GetResource('PICT', WF_PICT);
  104.         picHeight = (**picHand).picFrame.bottom - (**picHand).picFrame.top;
  105.         picWidth = (**picHand).picFrame.right - (**picHand).picFrame.left;
  106.         textRect.left = (textRect.left + textRect.right - picWidth) / 2;
  107.         textRect.top = (textRect.top + textRect.bottom - picHeight) / 2;
  108.         textRect.right = textRect.left + picWidth;
  109.         textRect.bottom = textRect.top + picHeight;
  110.         DrawPicture(picHand, &textRect);
  111.     }
  112. }
  113.  
  114.  
  115. METHODDEF pascal void
  116. PicItem (WindowPtr d, short dinum)
  117. {
  118.     short        itemType;
  119.     Rect        itemr;
  120.     PicHandle    picHand;
  121.  
  122.     if (gColorQuickdraw)
  123.         if (dinum == kJimPic)
  124.             picHand = (PicHandle)GetResource('PICT', JIM_16);
  125.         else
  126.             picHand = (PicHandle)GetResource('PICT', TAM_16);
  127.     else
  128.         if (dinum == kJimPic)
  129.             picHand = (PicHandle)GetResource('PICT', JIM_BW);
  130.         else
  131.             picHand = (PicHandle)GetResource('PICT', TAM_BW);
  132.     
  133.     GetDItem(d, dinum, &itemType, &gHandleSink, &itemr);
  134.     itemr.right = itemr.left + ((**picHand).picFrame.right - (**picHand).picFrame.left);
  135.     itemr.bottom = itemr.top + ((**picHand).picFrame.bottom - (**picHand).picFrame.top);
  136.     
  137.     DrawPicture(picHand, &itemr);
  138.     
  139.     ReleaseResource((Handle)picHand);
  140. }
  141.  
  142.  
  143. METHODDEF pascal void
  144. AboutScrollBar (ControlHandle theControl, short thePart)
  145. {
  146.     short    currentVal;
  147.     short    maxVal;
  148.     int        linesToScroll;
  149.     int        totalHeight;
  150.     int        windHeight;
  151.  
  152.     currentVal = GetCtlValue(theControl);
  153.     maxVal = GetCtlMax(theControl);
  154.     switch (thePart) {
  155.         case inUpButton:
  156.             if (gHelpLine > 1) {
  157.                 gHelpLine--;
  158.                 TEScroll(0, TEGetHeight(gHelpLine, gHelpLine, gHelpText), gHelpText);
  159.             }
  160.             break;
  161.         case inDownButton:
  162.             if (gHelpLine < maxVal) {
  163.                 TEScroll(0, -TEGetHeight(gHelpLine, gHelpLine, gHelpText), gHelpText);
  164.                 gHelpLine++;
  165.             }
  166.             break;
  167.         case inPageUp:
  168.             windHeight = (**gHelpText).viewRect.bottom - (**gHelpText).viewRect.top;
  169.             windHeight -= TEGetHeight(gHelpLine, gHelpLine, gHelpText);
  170.             totalHeight = 0;
  171.             linesToScroll = 0;
  172.             while ((totalHeight <= windHeight)  &&  (gHelpLine - (linesToScroll-1) > 0)) {
  173.                 linesToScroll++;
  174.                 totalHeight += TEGetHeight(gHelpLine-linesToScroll, gHelpLine-linesToScroll, gHelpText);
  175.             }
  176.             linesToScroll -= 1;        /* Ignore partial line */
  177.             TEScroll(0, TEGetHeight(gHelpLine-linesToScroll, gHelpLine-1, gHelpText), gHelpText);
  178.             gHelpLine -= linesToScroll;
  179.             break;
  180.         case inPageDown:
  181.             windHeight = (**gHelpText).viewRect.bottom - (**gHelpText).viewRect.top;
  182.             totalHeight = 0;
  183.             linesToScroll = 0;
  184.             while ((totalHeight <= windHeight)  &&  (gHelpLine + linesToScroll-2 < maxVal)) {
  185.                 totalHeight += TEGetHeight(gHelpLine+linesToScroll, gHelpLine+linesToScroll, gHelpText);
  186.                 linesToScroll++;
  187.             }
  188.             linesToScroll -= 2;        /* Keep one line, ignore partial line */
  189.             TEScroll(0, -TEGetHeight(gHelpLine, gHelpLine+linesToScroll-1, gHelpText), gHelpText);
  190.             gHelpLine += linesToScroll;
  191.             break;
  192.     }
  193.  
  194.     SetCtlValue(theControl, gHelpLine);    
  195. }
  196.  
  197.  
  198. METHODDEF pascal Boolean
  199. about_dialog_filter (DialogPtr d, EventRecord *theEvent, short *item)
  200. {
  201.     short    itemType;
  202.     Handle    itemHandle;
  203.     Rect    itemRect;
  204.     Point    mouseLoc;
  205.     short    thePart;
  206.     char    theKey;
  207.     Boolean    command;
  208.     Boolean    alreadyHandled=FALSE;
  209.     ControlHandle    theControl;
  210.     
  211.     SetPort(d);
  212.     
  213.     if (ReturnEnterEscape(d, theEvent, item))
  214.         return TRUE;
  215.     
  216.     switch (theEvent->what) {
  217.         case mouseDown:
  218.             mouseLoc = (theEvent->where);
  219.             GlobalToLocal(&mouseLoc);
  220.             
  221.             GetDItem(d, kAboutScroll, &itemType, &itemHandle, &itemRect);
  222.             if (PtInRect(mouseLoc, &itemRect)) {
  223.                 
  224.                 thePart = FindControl(mouseLoc, d, &theControl);
  225.                 /* if the hit was in an arrow or page area, we'll handle it */
  226.                 if (thePart != inThumb) {
  227.                     TrackControl(theControl, mouseLoc, (ProcPtr)AboutScrollBar);
  228.                     alreadyHandled = TRUE;
  229.                 }
  230.             }
  231.             break;
  232.     }
  233.  
  234.     /* If we have already handled the event, pass back a nullEvent to keep ModalDialog from
  235.      * returning.  Otherwise, if we are using the NEW dialog manager, we need to call the
  236.      * standard filter proc.
  237.      */
  238.     if (alreadyHandled) {
  239.         theEvent->what = nullEvent;
  240.         return FALSE;        /* tricks the dialog manager into handling a null event */
  241.     }
  242.     
  243.     if (gNewDialogMgr) {
  244.         ModalFilterProcPtr    theModalProc;
  245.                 
  246.         if (GetStdFilterProc(&theModalProc) == noErr)
  247.             return theModalProc(d, theEvent, item);
  248.     }
  249.     
  250.     return FALSE;
  251. }
  252.  
  253.  
  254. GLOBAL void
  255. DoAboutBox ()
  256. {
  257.     WindowPtr        AboutBox;
  258.     Rect            textRect;
  259.     short            item;
  260.     Rect            itemr;
  261.     short            itemType;
  262.     Handle            hTEXT;
  263.     StScrpHandle    hstyl;
  264.     int                windHeight;
  265.     int                totalHeight;
  266.     int                lastPageLines;
  267.     int                newLine;
  268.     Boolean            creditMode=FALSE;
  269.  
  270.     if (gAppleEvents)
  271.         if (AEInteractWithUser(kAEDefaultTimeout, NULL, NULL)) {
  272.             SysBeep(0L);
  273.             return;
  274.         }
  275.         
  276.     gHelpText = NULL;
  277.  
  278.     if (!gNewDialogMgr)
  279.         CenterDialog(ABOUT_DLOG, NULL);
  280.     AboutBox = GetNewDialog(ABOUT_DLOG, NULL, (WindowPtr)-1);
  281.     SetPort(AboutBox);
  282.  
  283.     /* If we are using the NEW dialog manager, it will handle the OK and CANCEL keyboard
  284.      * equivalents.  It will also border the OK button for us.  We don't wan't it to
  285.      * track the cursor since we have a "disabled" editText item.
  286.      */
  287.     if (gNewDialogMgr) {
  288.         SetDialogDefaultItem(AboutBox, ok);
  289.         SetDialogCancelItem(AboutBox, ok);
  290.         
  291.         GetDItem(AboutBox, kAboutOKBorder, &item, &gHandleSink, &itemr);
  292.         SetDItem(AboutBox, kAboutOKBorder, item, (Handle)NOPRoutine, &itemr);
  293.     } else {
  294.         GetDItem(AboutBox, kAboutOKBorder, &item, &gHandleSink, &itemr);
  295.         SetDItem(AboutBox, kAboutOKBorder, item, (Handle)BorderDefault, &itemr);
  296.     }
  297.  
  298.     GetDItem(AboutBox, kAboutText, &item, &gHandleSink, &textRect);
  299.     SetDItem(AboutBox, kAboutText, item, (Handle)AboutTextItem, &textRect);
  300.     
  301.     HideDItem(AboutBox, kJimPic);
  302.     HideDItem(AboutBox, kTamPic);
  303.  
  304.     ShowWindow(AboutBox);
  305.     DrawDialog(AboutBox);
  306.     
  307.     InsetRect(&textRect, 4, 2);
  308.     gHelpText = TEStylNew(&textRect, &textRect);
  309.     gHelpLine = 1;
  310.  
  311.     hTEXT = GetResource('TEXT', ABOUT_TEXT);
  312.     hstyl = (StScrpHandle)GetResource('styl', ABOUT_TEXT);
  313.     
  314.     HLock(hTEXT);
  315.     TEStylInsert(*hTEXT, SizeResource(hTEXT), hstyl, gHelpText);
  316.     HUnlock(hTEXT);
  317.  
  318.     ReleaseResource(hTEXT);
  319.     ReleaseResource((Handle)hstyl);
  320.  
  321.     windHeight = (**gHelpText).viewRect.bottom - (**gHelpText).viewRect.top;
  322.     totalHeight = 0;
  323.     lastPageLines = 0;
  324.     while (totalHeight <= windHeight) {
  325.         totalHeight += TEGetHeight((**gHelpText).nLines-lastPageLines, (**gHelpText).nLines-lastPageLines, gHelpText);
  326.         lastPageLines++;
  327.     }
  328.     lastPageLines -= 1;        /* Ignore partial line */
  329.     
  330.     SetCtlMax(CITEMH(AboutBox, kAboutScroll), (**gHelpText).nLines-(lastPageLines-1));
  331.  
  332.     GetDItem(AboutBox, kJimPic, &item, &gHandleSink, &itemr);
  333.     SetDItem(AboutBox, kJimPic, item, (Handle)PicItem, &itemr);
  334.     GetDItem(AboutBox, kTamPic, &item, &gHandleSink, &itemr);
  335.     SetDItem(AboutBox, kTamPic, item, (Handle)PicItem, &itemr);
  336.     
  337.     BeginUpdate(AboutBox);
  338.     EndUpdate(AboutBox);
  339.     GetDItem(AboutBox, kAboutOKBorder, &item, &gHandleSink, &itemr);
  340.     InvalRect(&itemr);
  341.     
  342.     for (;;) {
  343.         ModalDialog(about_dialog_filter, &item);
  344.         if (item == ok)
  345.             break;
  346.             
  347.         switch (item) {
  348.             case kAboutCredit:
  349.                 TEDispose(gHelpText);
  350.                 gHelpText = NULL;
  351.                 
  352.                 if (creditMode) {
  353.                     HideDItem(AboutBox, kJimPic);
  354.                     HideDItem(AboutBox, kTamPic);
  355.                     GetDItem(AboutBox, kAboutText, &item, &gHandleSink, &textRect);
  356.                     EraseRect(&textRect);
  357.                     textRect.left -= CREDIT_OFFSET;
  358.                     SetDItem(AboutBox, kAboutText, item, (Handle)AboutTextItem, &textRect);
  359.                     
  360.                     SetCTitle(CITEMH(AboutBox, kAboutCredit), "\pCredits...");
  361.                     
  362.                     AboutTextItem(AboutBox, kAboutText);
  363.                     
  364.                     InsetRect(&textRect, 4, 2);
  365.                     gHelpText = TEStylNew(&textRect, &textRect);
  366.                     gHelpLine = 1;
  367.                 
  368.                     hTEXT = GetResource('TEXT', ABOUT_TEXT);
  369.                     hstyl = (StScrpHandle)GetResource('styl', ABOUT_TEXT);
  370.                 } else {
  371.                     ShowDItem(AboutBox, kJimPic);
  372.                     ShowDItem(AboutBox, kTamPic);
  373.                     GetDItem(AboutBox, kAboutText, &item, &gHandleSink, &textRect);
  374.                     EraseRect(&textRect);
  375.                     textRect.left += CREDIT_OFFSET;
  376.                     SetDItem(AboutBox, kAboutText, item, (Handle)AboutTextItem, &textRect);
  377.                     
  378.                     SetCTitle(CITEMH(AboutBox, kAboutCredit), "\pHelp...");
  379.                     
  380.                     AboutTextItem(AboutBox, kAboutText);
  381.                     PicItem(AboutBox, kJimPic);
  382.                     PicItem(AboutBox, kTamPic);
  383.                     
  384.                     InsetRect(&textRect, 4, 2);
  385.                     gHelpText = TEStylNew(&textRect, &textRect);
  386.                     gHelpLine = 1;
  387.                 
  388.                     hTEXT = GetResource('TEXT', ABOUT_CREDIT);
  389.                     hstyl = (StScrpHandle)GetResource('styl', ABOUT_CREDIT);
  390.                 }
  391.  
  392.                 HLock(hTEXT);
  393.                 TEStylInsert(*hTEXT, SizeResource(hTEXT), hstyl, gHelpText);
  394.                 HUnlock(hTEXT);
  395.             
  396.                 ReleaseResource(hTEXT);
  397.                 ReleaseResource((Handle)hstyl);
  398.             
  399.                 windHeight = (**gHelpText).viewRect.bottom - (**gHelpText).viewRect.top;
  400.                 totalHeight = 0;
  401.                 lastPageLines = 0;
  402.                 while (totalHeight <= windHeight) {
  403.                     totalHeight += TEGetHeight((**gHelpText).nLines-lastPageLines, (**gHelpText).nLines-lastPageLines, gHelpText);
  404.                     lastPageLines++;
  405.                 }
  406.                 lastPageLines -= 1;        /* Ignore partial line */
  407.                 
  408.                 SetCtlMax(CITEMH(AboutBox, kAboutScroll), (**gHelpText).nLines-(lastPageLines-1));
  409.                 SetCtlValue(CITEMH(AboutBox, kAboutScroll), gHelpLine);
  410.  
  411.                 BeginUpdate(AboutBox);        /* We've done all of the necessary updating */
  412.                 EndUpdate(AboutBox);        /*  this will eliminate flicker                */
  413.                 GetDItem(AboutBox, kAboutOKBorder, &item, &gHandleSink, &itemr);
  414.                 InvalRect(&itemr);
  415.  
  416.                 creditMode = !creditMode;
  417.                 break;
  418.  
  419.             case kAboutScroll:
  420.                 /* take care of the thumb */
  421.                 newLine = GetCtlValue(CITEMH(AboutBox, kAboutScroll));
  422.                 if (newLine > gHelpLine)
  423.                     TEScroll(0, -TEGetHeight(gHelpLine, newLine-1, gHelpText), gHelpText);
  424.                 else
  425.                     TEScroll(0, TEGetHeight(gHelpLine-1, newLine, gHelpText), gHelpText);
  426.                 gHelpLine = newLine;
  427.                 break;
  428.         }
  429.     }
  430.  
  431.     DisposDialog(AboutBox);
  432.     InitCursor();
  433.     TEDispose(gHelpText);
  434.     gHelpText = NULL;
  435. }
  436.  
  437.  
  438. GLOBAL OSErr
  439. xFSMakeFSSpec (
  440.     short                vRefNum,
  441.     long                dirID,
  442.     ConstStr255Param    fileName,
  443.     FSSpecPtr            spec)
  444. {
  445.     OSErr    err;
  446.  
  447.     if (gFSSpecSupport)
  448.         err = FSMakeFSSpec(vRefNum, dirID, fileName, spec);
  449.     else {
  450.         Str255        tempName;
  451.         CInfoPBRec    pb;
  452.         
  453.         COPY(tempName, fileName);
  454.         
  455.         pb.hFileInfo.ioCompletion    = nil;
  456.         pb.hFileInfo.ioNamePtr        = tempName;
  457.         pb.hFileInfo.ioVRefNum        = vRefNum;
  458.         pb.hFileInfo.ioFDirIndex    = 0;
  459.         pb.hFileInfo.ioDirID        = dirID;
  460.         err = PBGetCatInfo(&pb, FALSE);
  461.         
  462.         spec->vRefNum = vRefNum;
  463.         spec->parID = dirID;
  464.         COPY(spec->name, pb.hFileInfo.ioNamePtr);
  465.     }
  466.     return err;
  467. }
  468.  
  469.  
  470. GLOBAL OSErr
  471. xFSpCreateResFile (
  472.     const FSSpec    *spec,
  473.     OSType            creator,
  474.     OSType            fileType,
  475.     ScriptCode        scriptTag)
  476. {
  477.     OSErr    err;
  478.  
  479.     if (gFSSpecSupport) {
  480.         FSpCreateResFile(spec, creator, fileType, scriptTag);
  481.         err = ResError();
  482.     } else {
  483.         HParamBlockRec    pb;    
  484.         Str255            tempName;
  485.         
  486.         FSSpecToName(*spec, tempName);
  487.         CreateResFile(tempName);
  488.         err = ResError();
  489.         
  490.         if (err == noErr) {
  491.             pb.fileParam.ioCompletion    = nil;
  492.             pb.fileParam.ioNamePtr        = (unsigned char *)spec->name;
  493.             pb.fileParam.ioVRefNum        = spec->vRefNum;
  494.             pb.fileParam.ioDirID        = spec->parID;
  495.             pb.fileParam.ioFDirIndex    = 0;
  496.             err = PBHGetFInfo(&pb, FALSE);
  497.             if (err == noErr) {
  498.                 pb.fileParam.ioDirID                = spec->parID;
  499.                 pb.fileParam.ioFlFndrInfo.fdType    = fileType;
  500.                 pb.fileParam.ioFlFndrInfo.fdCreator    = creator;
  501.                 err = PBHSetFInfo(&pb, FALSE);
  502.             }
  503.         }
  504.     }
  505.     return err;
  506. }
  507.  
  508.  
  509. GLOBAL short
  510. xFSpOpenResFile (
  511.     const FSSpec    *spec,
  512.     SignedByte        permission)
  513. {
  514.     short    refNum;
  515.  
  516.     if (gFSSpecSupport) {
  517.         refNum = FSpOpenResFile(spec, permission);
  518.     } else {
  519.         Str255    tempName;
  520.         
  521.         FSSpecToName(*spec, tempName);
  522.         refNum = OpenRFPerm(tempName, spec->vRefNum, permission);
  523.     }
  524.     return refNum;
  525. }
  526.